import upload from '../../utils/upload.js';
import request from '../../utils/http.js';
import Toast from '@vant/weapp/toast/toast';
import utils from '../../utils/util.js'
Page({
  data: {
    uploadType:0,//0  拍摄图片或影响
    imageList:[],
    describtText:'',
    addIconisShow:true,
    video:'',
    indexImg:'',
    imgCount:9,
    lock:true,
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    let type = options.type; // 0  上传照片   1  上传视频
    this.setData({
      uploadType: type,
      babyId: options.babyId
    })
  },
  addImages(){
    let that = this;
    let type = that.data.uploadType;
    let imgCount = that.data.imgCount;
    //在上个页面已经选择了上传照片还是视频
    //根据type    0   上传照片   1  上传视频
      //判断用户从手机选择还是拍摄
      wx.showActionSheet({
        itemList: ['拍摄','从手机相册选择'],
        success(result){
          let tapIndex = result.tapIndex;
          //0拍摄   1手机选择
          if (tapIndex == 0){ //拍摄
            if (type == 0) {
              //需要拍摄且图片
              wx.chooseImage({
                count: 9,
                sourceType: ['camera'],
                success: function(res) {
                  let imgs = res.tempFilePaths;
                  let imgslen = imgs.length;
                  let imageList = that.data.imageList;
                  let imageListLen = that.data.imageList.length;
                  if ((imgslen + imageListLen)<=9){
                    that.setData({
                      imageList: that.data.imageList.concat(imgs)
                    })
                    if ((imgslen + imageListLen) ==9){
                      that.setData({
                        addIconisShow: false
                      })
                    }
                  }else{
                    Toast('最多只能选择9张图片哦~');
                  }
                },
              })
            } else if (type == 1){
              //需要拍摄且选择视频
              wx.chooseVideo({
                sourceType: ['camera'],
                maxDuration: 15,
                compressed:false,
                camera: 'back',
                success(e) {
                  that.setData({
                    video: e.tempFilePath,
                    addIconisShow:false
                  })
                }
              })
            }
          } else if (tapIndex == 1){//从手机中选择
            if (type == 0) {
              //手机中选择且图片
              wx.chooseImage({
                count: imgCount,
                sourceType: ['album'],
                success: function (res) {
                  let imgs = res.tempFilePaths;
                  let imgslen = imgs.length;
                  let imageList = that.data.imageList;
                  let imageListLen = that.data.imageList.length;
                  if ((imgslen + imageListLen) <= 9) {
                    that.setData({
                      imageList: that.data.imageList.concat(imgs),
                      imgCount: 9 - (imgslen + imageListLen)
                    })
                    if ((imgslen + imageListLen) == 9) {
                      that.setData({
                        addIconisShow: false,
                        imgCount:0
                      })
                    }
                  } else {
                    Toast('最多只能选择9张图片哦~');
                  }
                },
              })
            } else if (type == 1) {
              //手机中选择且拍摄视频
              wx.chooseVideo({
                sourceType: ['album'],
                maxDuration: 15,
                compressed:false,
                camera: 'back',
                success(e) {
                  that.setData({
                    video: e.tempFilePath,
                    addIconisShow: false
                  })
                }
              })
            }
          }
        }
      })
  },
  //图片预览
  previewImage(e){
    let current = e.target.dataset.src;
    wx.previewImage({
      current: current, // 当前显示图片的http链接  
      urls: this.data.imageList // 需要预览的图片http链接列表  
    })
  },
  deleteImg(e){//删除图片
    console.log(e)
    let imgs = this.data.imageList;
    let itemIndex = e.currentTarget.dataset.currentindex;
    imgs.splice(itemIndex, 1);
    if (imgs.length >= 9){
      this.setData({
        imageList: imgs,
        addIconisShow:false
      })
    }else{
      this.setData({
        imageList: imgs,
        addIconisShow: true
      })
    }
    
    console.log(this.data.imageList)
  },
  // 删除视频
  videoDelete(){
    wx.showModal({
      title: '警告',
      content: '确定要删除该视频吗',
      success: res => {
        if (res.confirm) {
          this.setData({
            video: '',
            addIconisShow: true
          })
        }
      }
    })
  },
  bindText(e){
    this.setData({
      describtText:e.detail.value
    })
  },
  bindblur(e) {
    let describtText = e.detail.value;
    if (describtText.length > 100){
      Toast('输入的内容不能超过100个字哦')
      this.setData({
        description: e.detail.value
      })
    }else{
      this.setData({
        description: e.detail.value
      })
    }
  },
  publishRecord(){
    let that = this;
    if(that.data.lock){
      that.setData({
        lock:false
      })
      let describtText = that.data.describtText;
      let imgs = that.data.imageList;
      let video = that.data.video;
      let arr = [];
      if (describtText == '' && imgs.length == 0 && video == '') {
        Toast('内容不能为空~');
        return
      } else if (describtText.length >= 100) {
        Toast('输入的内容不能超过100个字哦')
        return
      } else {
        wx.showLoading({
          title: '上传中...',
        })
        if (that.data.uploadType == 0 && imgs.length>0) {//上传的是图片
          for (let i = 0; i < imgs.length; i++) {
            arr.push(upload(imgs[i], "pic_root"))
          }
        } else if (that.data.uploadType == 1 && that.data.video!= '') {//上传的是视频
          arr.push(upload(that.data.video, "video"))
        }
        if (imgs.length > 0 || that.data.video != ''){
          Promise.all(arr).then(res => {
            let urls;
            if (that.data.uploadType == 0) {
              urls = res.map(item => {
                return item.url
              })
            } else {
              urls = res[0].url;
            }
            // growthType //上传数据类型 0 图片  1视频
            utils.HttpRequst('true', '/baby-cloud-base/mobileGrowthVideo/addItem', 'post', {
              babyId: that.data.babyId,
              description: that.data.describtText,
              picJson: urls,
              growthType: that.data.uploadType
            }, res => {
              if (res.data.code == 200) {
                that.setData({
                  lock: true
                })
                wx.hideLoading()
                wx.navigateBack({
                  delta: 1,
                })
              }
            })
          })
        }else{ //只上传文字
          utils.HttpRequst('true', '/baby-cloud-base/mobileGrowthVideo/addItem', 'post', {
            babyId: that.data.babyId,
            description: that.data.describtText,
            picJson: '',
            growthType:0
          }, res => {
            if (res.data.code == 200) {
              that.setData({
                lock: true
              })
              wx.hideLoading()
              wx.navigateBack({
                delta: 1,
              })
            }
          })
        }
       
      }
    }
    
  }
})